#!/usr/bin/env bash
################################################################################
# CLIMAX Candy Suite INSTALL Script
#
# Copyright (c) 2008-2011 David Christian Liedle <david.liedle@gmail.com>
# All rights reserved. Released under the terms of the MIT Open Source License.
# Visit http://code.google.com/p/climax/ to view this project's home page/repo.
#


################################################################################
# A few conveniences for this script before it begins
# Why? Just to save typing later. ;]

ROWDASH="--------------------------------------------------------------------------------"

################################################################################
# Begin INSTALL Script:
# Why? Well, that's why we're running it, of course!

clear
echo
echo "The CLIMAX Candy Suite INSTALL Script is beginning..."
echo "( Please make sure you're running it with root permissions to edit /usr/local )"
echo $ROWDASH
echo

################################################################################
# CHECK FOR PREVIOUS INSTALLS
# Why? Because I like lots of feedback from my installers on what I have in place

echo -n "First, I will check for existing files: "

if [ -d /usr/local/lib/climax ]; then
    if [ -d /usr/local/candy ]; then
        echo "done."
        echo "I found both the CLIMAX frameworks folder and the Candy Suite."
    else
        echo "done."
        echo "We have the CLIMAX frameworks, but not the Candy Suite."
        echo -n "Creating the Candy Suite folder in /usr/local/candy :"
        mkdir /usr/local/candy
        echo "done."
    fi
else
    if [ -d /usr/local/candy ]; then
        echo "done."
        echo "We have the Candy Suite folder, but not the CLIMAX frameworks."
        echo -n "Creating the CLIMAX frameworks folder in /usr/local/lib/climax :"
        mkdir /usr/local/lib/climax
        echo "done."
    else
        echo "done."
        echo "No previous installation detected..."
        echo -n "I will now create /usr/local/lib/climax for the frameworks: "
        mkdir /usr/local/lib/climax
        echo "done."
        echo -n "And now we'll make /usr/local/candy for the Candy Suite: "
        mkdir /usr/local/candy
        echo "done."
    fi
fi

################################################################################
# DOUBLE-CHECK THAT WE'RE READY TO INSTALL
# Why? If there is a permissions error, we can catch it here

if [ -d /usr/local/lib/climax ]; then
    if [ -d /usr/local/candy ]; then
        echo "Alright, we're all set with the directories on this machine!"
    else
        echo "I'm sorry, there seems to be a problem with creating /usr/local/candy"
        echo "I can't continue... Please make sure you're running this INSTALL script with either"
        echo "sudo ./INSTALL, or that you are root as you run ./INSTALL"
        echo "This is to allow the script to create directories in the protected areas under"
        echo "/usr/local/lib/climax and /usr/local/candy"
        echo "I have to exit now... Try again!"
        exit 1
    fi
else
    echo "I'm sorry, there seems to be a problem with creating /usr/local/lib/climax"
    echo "I can't continue... Please make sure you're running this INSTALL script with either"
    echo "sudo ./INSTALL, or that you are root as you run ./INSTALL"
    echo "This is to allow the script to create directories in the protected areas under"
    echo "/usr/local/lib/climax and /usr/local/candy"
    echo "I have to exit now... Try again!"
    exit 1
fi

################################################################################
# MAKE SURE WE'VE GOT OUR FILES READY TO COPY
# Why? It's pointless to continue without them.. ;P

echo "Now I need to check if we have a good copy of the installation files..."
echo -n "Checking for ./climax/ : "
if [ -d ./climax ]; then
    echo "done."
    echo "./climax/ has been found."
else
    echo "done."
    echo "Nope, I'm not seeing ./climax/"
    echo "Please make sure you are in the download folder as you execute this!"
    echo "I have to exit now... Try again!"
    exit 1
fi
echo -n "Checking for ./candy/ : "
if [ -d ./candy ]; then
    echo "done."
    echo "./candy/ has been found."
else
    echo "done."
    echo "Nope, I'm not seeing ./candy/"
    echo "Please make sure you are in the download folder as you execute this!"
    echo "I have to exit now... Try again!"
    exit 1
fi

################################################################################
# COPY ALL FILES TO THEIR APPROPRIATE DIRECTORIES
# Now we're getting down to business. All that other info above was just FYI.
echo "I'll now copy the files to their appropriate directories."
echo -n "  Copying CLIMAX frameworks...."
echo
cp -R ./climax/* /usr/local/lib/climax/
echo "done!"
echo -n "  Copying Candy Suite...."
cp ./candy/* /usr/local/candy/
echo "done!"

################################################################################
# CHECK THAT THE COPYING WORKED
# Why? Because if it didn't, we deserve to know that without further effort.
echo
echo "INSTALLED!"
echo
echo $ROWDASH
echo
echo "I will now list the contents of each directory to confirm."
echo "If this listing is empty, something went wrong. You should now see the contents"
echo "of /usr/local/lib/climax/"
ls /usr/local/lib/climax/
echo
echo "And here is a listing of /usr/local/candy/"
ls /usr/local/candy/
echo


################################################################################
# CONFIRM AND OFFER FURTHER GUIDANCE / NEXT STEPS
# Why? Because software is no fun if you don't know what to do with it. ;]

echo $ROWDASH
echo "The CLIMAX Candy Suite has finished its work. SUCCESS!"
echo
echo "Documentation is in ./docs/ as I write it. Feel free to pitch in as an author!"
echo "The most current info on the status and usage of The CLIMAX Candy Suite"
echo "is at the official Google Code site:"
echo
echo "  http://code.google.com/p/climax/"
echo
echo "Here's the bare-bones basics you'll need to get started:"
echo "  1) Make sure that /usr/local/candy/ is in your PATH"
echo "     (That way, you can use the Candy Suite from any directory on your system.)"
echo "  2) Include the appropriate CLIMAX framework for the language you are using as"
echo "     the first line of the script you write, after the shebang and any comments."
echo
echo $ROWDASH
echo
echo "Have fun! ;]"
exit 0
